import React from 'react';
import ImageLazy from '../commons/ImageLazy';
import NavLink from '../commons/NavLink';
import { connect } from 'react-redux';
import {hashHistory } from 'react-router'
import {get_add_shopcar,pro_order_list} from '../../actions';
import {getHosImg,getDocImg,getServImg} from '../../actions/ActionTypes';

const user_dele = require('!url?limit=8192&name=static/img/[hash:6].[ext]!../../static/img/user_dele.png');
const duihao = require('!url?limit=8192&name=static/img/[hash:6].[ext]!../../static/img/duihao.png');


let mydefault = React.createClass({
  componentDidMount:function(){
    const {dispatch} = this.props;
    dispatch(get_add_shopcar(this.props.userId));
    let _this = this;
// (user_phone,cart_id,user_id,item_price,item_num,item_id,hospital_id,doctor_id,item_name,order_from,item_image)
    $('.fix_total a').click(function(){
      _this.dele_pro()
    });
    this.get_total();
  },
  dele_pro:function(obj){
    const {dispatch} = this.props;
    let myconfig = [],
        _this = this;
    _this.props.servDet_arr.forEach(function(item,index){
        // console.log(item.cartId);
        let config_arr = {
          "cart_id":0,
          "user_id":_this.props.userId,
          "item_price":item.appointmentPrice,
          "item_num":1,
          "item_id":item.itemId,
          "hospital_id":item.hospitalId,
          "doctor_id":item.doctorId,
          "item_name":item.itemName,
          "order_from":'2',
          "item_image":item.itemImage
        };
        myconfig.push(config_arr)
        let query = item.cartId;
        pro_order_list(_this.props.userTel,myconfig)
    })
  },
  componentDidUpdate:function(){
    this.get_total();
  },
  componentWillUnmount:function(){
    // alert(0)
  },
  in_array(search,array){
      for(var i in array){
          if(array[i]==search){
              return true;
          }
      }
      return false;
  },
  get_total:function(){
    this.props.total = 0;
    let that= this;
    $('.item_wrapper .item').each(function(){
      that.props.total += $(this).find('.price').text() * 1;
    });
    $('.fix_total .total_price').text(that.props.total)
  },
  render:function(){
    let setDet = this.props.servDet_arr || [],
        _this = this;
    return(
      <div className="u_shop_car confirm_order box">
        <div className="item_wrapper">
          {
            setDet.map(function(item,index){
              // console.log(_this.in_array(index,checkedArr));
              let imgSrc = getServImg(item.itemImage,'_200_200',item.hospitalId,item.itemId)
                  return(
                    <div className="item border_b1">
                      <div className="wrapper clearfix">
                        <h4>{item.itemName}·{item.subtitle}</h4>
                        <div className="mid">
                          {/* <ImageLazy src="https://dummyimage.com/120x120/b293a4/fff" style={{height:"1.2rem"}}  /> */}
                          <em style={{"background-image":"url("+ imgSrc + ")"}}></em>
                          <div>
                            医院：{item.hospitalName}<br/><br/>
                            包含：{item.itemName}
                          </div>
                        </div>
                        <h5>预约金：¥ <font className="price">{item.appointmentPrice}</font></h5>
                        {/* <div className="num">x<font>{item.itemNum}</font></div> */}
                      </div>
                    </div>
                  )
            })
          }

          <div className="connect border_b1"><span>手机号码</span><span>{_this.props.userTel}</span></div>
        </div>

        <div className="fix_total border_t1 box">
          <div className="top">
            <span><var></var>合计：¥ <font className="total_price">0</font></span>
          </div>
          <a href="javascript:void(0)">确认订单</a>

        </div>
      </div>
    )
  }
})

let mapStateToProps = (state) => {
  let userId = state.user.userId || 0,
      userTel = state.user.userTel || '',
      servDet_arr = state.user.mall_them_hos_servItem_det.itemList || [],
      user_shop_list = state.user.user_shop_list || [];
      console.log(servDet_arr);
  return{
    userId,
    userTel,
    total:0,
    servDet_arr
  }
}

export default connect(mapStateToProps)(mydefault);
